﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThermalEquipmentDesign.Model.PlateAmoniac
{
    public class PlateAmoniacModel
    {
        public PlateAmoniacResult result = new PlateAmoniacResult();
        public PlateAmoniac plateamoniac;

        /// <summary>
        /// Calculate Heat Canal Area
        /// </summary>
        /// <returns>Heat Canal Area</returns>
        private double HeatCanalArea()
        {
            double Fik = Math.PI * plateamoniac.dbDiameter * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            return Math.Round(Fik,2);
        }

        /// <summary>
        /// Calculate Heat Manifolds Area
        /// </summary>
        /// <returns>Heat Manifolds Area</returns>
        private double HeatManifoldsArea()
        {
            double Fig = Math.PI * plateamoniac.dbLengthOfUnitary * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            return Math.Round(Fig,2);
        }

        /// <summary>
        /// Calculate Heat Unitary Area
        /// </summary>
        /// <returns>Heat Unitary Area</returns>
        private double HeatUnitaryArea()
        {
            double Fik = Math.PI * plateamoniac.dbDiameter * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fig = Math.PI * plateamoniac.dbLength * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fil = Fik + Fig;
            return Math.Round(Fil,2);
        }

        /// <summary>
        /// Calculate Heat Exchange Area Outside
        /// </summary>
        /// <returns>Heat Exchange Area Outside</returns>
        private double HeatExchangeAreaOutside()
        {
            double Fak = Math.PI * plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fag = Math.PI * plateamoniac.dbLengthOfUnitary * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fac = Math.PI * (plateamoniac.dbLength - plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbCellInUnitary) * plateamoniac.dbCellInUnitary;
            double Fal = Fak + Fag + Fac;
            return Math.Round(Fal,2);
        }

        /// <summary>
        /// Calculate Reynolds Value
        /// </summary>
        /// <returns>Reynolds Value</returns>
        private double ReynoldsValue()
        {
            double Re = 0.2 * plateamoniac.dbLengthOfUnitary / (4.374 * Math.Pow(10, -6));
            return Re;
        }

        /// <summary>
        /// Calculate Nusselt Value
        /// </summary>
        /// <returns>Nusselt Value</returns>
        private double NusseltValue()
        {
            double Re = 0.2 * plateamoniac.dbLengthOfUnitary / (4.374 * Math.Pow(10, -6));
            double Nu = 0.037 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.43);
            return Nu;
        }

        /// <summary>
        /// Calculate Heat Transfer Coefficient
        /// </summary>
        /// <returns>Heat Transfer Coefficient</returns>
        private double HeatTransferCoefficient()
        {
            double Re = 0.2 * plateamoniac.dbLengthOfUnitary / (4.374 * Math.Pow(10, -6));
            double Nu = 0.037 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.43);
            double Fik = Math.PI * plateamoniac.dbDiameter * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fig = Math.PI * plateamoniac.dbLength * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fil = Fik + Fig;
            double Fak = Math.PI * plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fag = Math.PI * plateamoniac.dbLengthOfUnitary * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fac = Math.PI * (plateamoniac.dbLength - plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbCellInUnitary) * plateamoniac.dbCellInUnitary;
            double Fal = Fak + Fag + Fac;
            double abi = Nu * 0.526 * Fal / (plateamoniac.dbLengthOfUnitary * Fil);
            return Math.Round(abi,1);
        }

        /// <summary>
        /// Calculate Density Water Salt
        /// </summary>
        /// <returns>Density Water Salt</returns>
        private double DensityWaterSalt()
        {
            double Re = 0.2 * plateamoniac.dbLengthOfUnitary / (4.374 * Math.Pow(10, -6));
            double Nu = 0.037 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.43);
            double Fik = Math.PI * plateamoniac.dbDiameter * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fig = Math.PI * plateamoniac.dbLength * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fil = Fik + Fig;
            double Fak = Math.PI * plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fag = Math.PI * plateamoniac.dbLengthOfUnitary * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fac = Math.PI * (plateamoniac.dbLength - plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbCellInUnitary) * plateamoniac.dbCellInUnitary;
            double Fal = Fak + Fag + Fac;
            double abi = Nu * 0.526 * Fal / (plateamoniac.dbLengthOfUnitary * Fil);
            double qFb = plateamoniac.dbDiviateTemperatureWaterSalt / (1 / abi + 0.0008);
            return Math.Round(qFb,0);
        }

        /// <summary>
        /// Calculate Density Heat NH3
        /// </summary>
        /// <returns>Density Heat NH3</returns>
        private double DensityHeatNH3()
        {
            double tmp = (27.3 + 0.04*plateamoniac.dbAmoniacBoiling);
            double qFR = Math.Pow(tmp,1.82)*Math.Pow(plateamoniac.dbDiameter,-0.436)*Math.Pow(plateamoniac.dbDiviateTemperatureWaterSalt,1.82);
            return qFR;
        }

        /// <summary>
        /// Calculate Heat Transfer Inside
        /// </summary>
        /// <returns>Heat Transfer Inside</returns>
        private double HeatTransferInside()
        {
            double Re = 0.2 * plateamoniac.dbLengthOfUnitary / (4.374 * Math.Pow(10, -6));
            double Nu = 0.037 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.43);
            double Fik = Math.PI * plateamoniac.dbDiameter * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fig = Math.PI * plateamoniac.dbLength * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fil = Fik + Fig;
            double Fak = Math.PI * plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fag = Math.PI * plateamoniac.dbLengthOfUnitary * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fac = Math.PI * (plateamoniac.dbLength - plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbCellInUnitary) * plateamoniac.dbCellInUnitary;
            double Fal = Fak + Fag + Fac;
            double abi = Nu * 0.526 * Fal / (plateamoniac.dbLengthOfUnitary * Fil);
            double qFb = plateamoniac.dbDiviateTemperatureWaterSalt / (1 / abi + 0.0008);
            double Fi = plateamoniac.dbCapacity / qFb;
            return Fi;
        }

        /// <summary>
        /// Calculate Number Unitary
        /// </summary>
        /// <returns>Number Unitary</returns>
        private double NumberUnitary()
        {
            double Re = 0.2 * plateamoniac.dbLengthOfUnitary / (4.374 * Math.Pow(10, -6));
            double Nu = 0.037 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.43);
            double Fik = Math.PI * plateamoniac.dbDiameter * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fig = Math.PI * plateamoniac.dbLength * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fil = Fik + Fig;
            double Fak = Math.PI * plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbHeight * plateamoniac.dbNumberCanal * plateamoniac.dbCellInUnitary;
            double Fag = Math.PI * plateamoniac.dbLengthOfUnitary * (plateamoniac.dbDiameterManifoldSteam + plateamoniac.dbDiameterManifoldWatery);
            double Fac = Math.PI * (plateamoniac.dbLength - plateamoniac.dbDiameterManifoldWatery * plateamoniac.dbCellInUnitary) * plateamoniac.dbCellInUnitary;
            double Fal = Fak + Fag + Fac;
            double abi = Nu * 0.526 * Fal / (plateamoniac.dbLengthOfUnitary * Fil);
            double qFb = plateamoniac.dbDiviateTemperatureWaterSalt / (1 / abi + 0.0008);
            double Fi = plateamoniac.dbCapacity / qFb;
            double Zo = Fi / Fil;
            return Zo;
        }

        /// <summary>
        /// Calculate Canal Section Area
        /// </summary>
        /// <returns>Canal Section Area</returns>
        private double CanalSectionArea()
        {
            double F = plateamoniac.dbCapacity / (plateamoniac.dbDiviateTemperatureWaterSalt * 2.91 * 1232 * 0.2);
            return F;
        }

        /// <summary>
        /// Calculate Plate Amoniac
        /// </summary>
        /// /// <param name="con">PlateAmoniac object</param>
        /// <returns>PlateAmoniacResult object</returns>
        public PlateAmoniacResult ImplementCalculation(PlateAmoniac pa)
        {
            plateamoniac = pa;
            result.DbCanalSectionArea = CanalSectionArea();
            result.DbDensityHeatNH3 = DensityHeatNH3();
            result.DbDensityWaterSalt = DensityWaterSalt();
            result.DbHeatCanalArea = HeatCanalArea();
            result.DbHeatExchangeAreaOutside = HeatExchangeAreaOutside();
            result.DbHeatManifoldsArea = HeatManifoldsArea();
            result.DbHeatTransferCoefficient = HeatTransferCoefficient();
            result.DbHeatTransferInside = HeatTransferInside();
            result.DbHeatUnitaryArea = HeatUnitaryArea();
            result.DbNumberUnitary = NumberUnitary();
            result.DbNusseltValue = NusseltValue();
            result.DbReynoldsValue = ReynoldsValue();
            return result;
        }
    }
}
